<template>
	<view>
			<view class="cu-list grid col-2 no-border">
			<view class="cu-item text-left">
				<text class="text-xs nowrap" style="margin-left: 9px;">{{i18n.wallet.total}}(BTC)</text>
				<view style="margin-left: 9px;">
					<view class="text-xxl row">{{btctotal}}</view>
				</view>
				<!-- <text class="text-xs nowrap" style="margin-left: 9px;">≈{{cnytotal}} CNY</text> -->
				<text class="text-xs nowrap" style="margin-left: 9px;">≈{{usdtotal}} USDT</text>

			</view>
			<view class="cu-item text-left">
			 

			</view>

		</view>
		 
		<view class="flex btt " style="padding-bottom: 10px;">
			<view class="flex-sub ">
				<button class="cu-btn bg-yellow" @tap="navTo('/pages/money/charge2')">{{i18n.wallet.charge}}</button>
			</view>
			<view class="flex-sub ">
				<button class="cu-btn  " @tap="navTogocash">{{i18n.wallet.cash}}</button>
			</view>
			<!-- <view class="flex-sub ">
				<button class="cu-btn tt-bg-yellow"
					@tap="navTo('/pages/money/transfers')">{{i18n.wallet.Transfer}}</button>
			</view> -->
			<!-- <view class="flex-sub ">
				<button class="cu-btn round bg-green" @tap="navTo('/pages/money/transfers3')">{{i18n.wallet.Transfer2}}</button>
			</view> -->


		</view>

		<view class="cu-list menu" style="margin-top: 5px;" v-for="(value,key,index) in myconfig.cointype" :key="key">
			<view class="cu-item arrow" @tap="caiwu(key)">
				<view class="content">
					<!-- <image :src="binfo[value].logo" class="png" mode="aspectFit"></image> -->

					<text class="text-grey">{{value}}</text>
				</view>
			</view>
			<view class="cu-list grid col-3 no-border">
				<view class="cu-item text-left">
					<text class="text-xs nowrap" style="margin-left: 9px;">{{i18n.wallet.available}}</text>
					<view style="margin-left: 9px;">
						<view class="text-df row">{{userinfo[key]}}</view>
					</view>
				</view>
				<view class="cu-item  ">
					<text>{{i18n.wallet.freeze}}</text>
					<view>
						<view class="text-df">{{userinfo[key+'freeze']}}</view>
					</view>

				</view>
				<!-- <view class="cu-item">
					<text>{{i18n.wallet.convert}}</text>
					<view>
						<view class="text-df">≈ {{cacny(userinfo[key],userinfo[key+'freeze'],value)}} CNY</view>
					</view>

				</view> -->
			</view>
		</view>


	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				binfo: {},
				cnytotal: 0,
				btctotal: 0,
				usdtotal: 0,
				coinarr: ['wall1', 'wall3', 'wall4', 'wall5', 'wall6']
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				myconfig: state => state.user.myconfig
			}),
			hasLogin() {
				return this.userinfo.id ? true : false
			},
			i18n() {
				return this.$t('message');
			},
			// cnyprice() {
			// 	return Math.round(this.close * this.myconfig.rate_cny2usdt * 100) / 100;
			// },
			// usdprice() {
			// 	return Math.round(this.close * this.myconfig.rate_usdt2usd * 100) / 100;
			// },
		},
		methods: {
			...mapActions(['getUserinfo']),
			tablang() {
				uni.setTabBarItem({
					index: 0,
					text: this.i18n.tabBar.home
				});
				uni.setTabBarItem({
					index: 1,
					text: this.i18n.tabBar.coin
				});
				uni.setTabBarItem({
					index: 2,
					text: this.i18n.tabBar.Wallet
				});
				uni.setTabBarItem({
					index: 3,
					text: this.i18n.tabBar.team
				});
				uni.setTabBarItem({
					index: 4,
					text: this.i18n.tabBar.my
				});
			},
			navTogocash(){
				uni.removeStorageSync('selectaddress');
				this.navTo('/pages/money/cash');
			},
			cacny(use, free, type) {
				let all = this.accAdd(use, free);
				let price = this.myconfig.rate_cny2usdt;
				// let price = this.binfo[type].price * this.myconfig.rate_cny2usdt;
				return Math.round(all * price * 100) / 100;
			},
			causd(use, free, type) {
				let all = this.accAdd(use, free);
				let price = this.myconfig.rate_usdt2usd;
				// let price = this.binfo[type].price * this.myconfig.rate_usdt2usd;
				return Math.round(all * price * 100) / 100;
			},
			catotal() {
				var total = 0;
				let coinarr = ['wall1'];
				coinarr.forEach((item, index) => {
					console.log(item)
					// total += this.cacny(this.userinfo[item], this.userinfo[item + 'freeze'], this.myconfig.cointype[item]);
					total += this.causd(this.userinfo[item], this.userinfo[item + 'freeze'], 'USDT');
				})

				// for (let key in this.myconfig.cointype) { 
				// 	total+=this.cacny(this.userinfo[key],this.userinfo[key+'freeze'],this.myconfig.cointype[key]);
				// }
				// this.cnytotal = total.toFixed(2);
				// this.btctotal = (this.cnytotal / this.binfo['BTC'].price / 7).toFixed(8);
				this.usdtotal = total.toFixed(2);
				this.btctotal = (this.usdtotal / this.binfo['btc'].price).toFixed(8);

			},
			navTo(url) {
				if (!this.userinfo.id) {
					url = '/pages/public/login';
				}
				uni.navigateTo({
					url
				})
			},
			caiwu(index) {
				uni.navigateTo({
					url: '/pages/money/finance?index=' + index
				})
			},
			getcoinlist() {
				//行情列表
				this.$http.post('btc.binfo').then(res => {
					this.binfo = res.data;
				})
			},
			//加法
			accAdd(arg1, arg2) {
				var r1, r2, m, n;
				try {
					r1 = arg1.toString().split(".")[1].length
				} catch (e) {
					r1 = 0
				}
				try {
					r2 = arg2.toString().split(".")[1].length
				} catch (e) {
					r2 = 0
				}
				m = Math.pow(10, Math.max(r1, r2));
				//last modify by deeka
				//动态控制精度长度
				n = (r1 >= r2) ? r1 : r2;
				var ree = ((arg1 * m + arg2 * m) / m).toFixed(n);
				return ree > 0 ? ree : 0;
			},

		},
		onLoad() {

			this.binfo = uni.getStorageSync('binfo');
			return Promise.all([this.getcoinlist()])
		},
		onShow() {
			this.tablang()
			uni.setNavigationBarTitle({
				title: this.i18n.wallet.title
			});
			if (!this.hasLogin) {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			} else {

				this.getUserinfo()
				//更新用户区块充币
				this.$http.post('user.updatetoken').then(res => {
					console.log(res)
				})
				this.binfo = uni.getStorageSync('binfo');
				this.catotal();
			}
		},
		onHide() {
			//缓存 
			uni.setStorage({
				key: 'binfo',
				data: this.binfo,
			})
		},
	}
</script>

<style lang="scss">
	.btt {
		background: $page-color-light;
	}

	.flex-sub button {
		width: 90%;
		margin-left: 5%;
	}
</style>
